﻿using ModernShopping.AppCode;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ModernShopping.WebUserControls
{
    public partial class UCCountDown : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void InitCountdown(int Count, string Title, string Message, int URL = -1, params string[] parameters)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<span class='notification-title'>{0}</span>");
            builder.Append("<br/>");
            builder.Append("<br/>");
            builder.Append("<span class='notification-message'>{1}</span>");
            ViewState.Add("Countdown", Count);
            ViewState.Add("Message", string.Format(builder.ToString(), Title, Message));
            ViewState.Add("URL", URL);
            if (parameters.Length > 0)
            {
                ViewState.Add("Parameters", parameters);
            }
        }

        public void InitCountdown(int Count, string Title, int URL = -1)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<br/>");
            builder.Append("<span class='notification-title'>{0}</span>");
            builder.Append("<br/>");
            builder.Append("<br/>");
            ViewState.Add("Countdown", Count);
            ViewState.Add("Message", string.Format(builder.ToString(), Title));
            ViewState.Add("URL", URL);
        }

        public void Start()
        {
            Timer1.Enabled = true;
        }
        public void Stop()
        {
            Timer1.Enabled = false;
            ModalPopupExtender.Hide();
        }
        protected void Timer1_Tick(object sender, EventArgs e)
        {
            int val = (int)ViewState["Countdown"];
            string Message = (string)ViewState["Message"];
            val--;
            if (val >= 0)
            {
                myDiv.InnerHtml = string.Format(Message, val);
                ViewState.Add("Countdown", val);
            }
            else
            {
                int url = (int)ViewState["URL"];
                if (url != -1)
                {
                    if (url == WebUtil.Page.HOME)
                    {
                        Response.GoToHomePage(Request);
                    }
                    else if (url == WebUtil.Page.GOOGLE_MAP)
                    {
                        Response.Redirect(Request.Url.AbsoluteUri);
                    }
                    else if (url == WebUtil.Page.ADD_PRODUCT)
                    {
                        Response.GoToAddProductPage(Request);
                    }
                    else if (url == WebUtil.Page.UPDATE_PRODUCT)
                    {
                        string[] parameters = ViewState["Parameters"] as string[];
                        if (parameters.Length > 0)
                        {
                            int ProductCode = int.Parse(parameters[0]);
                            Response.GoToUpdateProductPage(Request, ProductCode);
                        }
                        else
                        {
                            Response.GoToUpdateProductPage(Request);
                        }
                    }
                    else if (url == WebUtil.Page.PRODUCT_IN_STORE)
                    {
                        Response.GoToProductInStorePage(Request);
                    }
                    else if (url == WebUtil.Page.HOME_USER)
                    {
                        Response.GoToUserHomePage(Request);
                    }
                    else if (url == WebUtil.Page.LOGIN)
                    {
                        Response.GoToLoginPage(Request);
                    }
                }
                Stop();
            }
        }

        public void ShowPopup()
        {
            ModalPopupExtender.Show();
        }
        public void HidePopup()
        {
            ModalPopupExtender.Hide();
        }
    }
}